package com.tarena.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tarena.entity.User;
import com.tarena.service.UserService;

import net.sf.json.JSONObject;

/**
 * liubiao
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	private PrintWriter out;

	//增加用户
	@RequestMapping("/saveUser.do")
	public void save(User user, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("save");
		boolean save = userService.save(user);
		out = res.getWriter();
		out = res.getWriter();
		if (save) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}
	
	//删除用户
	@RequestMapping("/deleteUser.do")
	public void delete(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("delete");
		boolean delete = userService.delete(Integer.parseInt(req.getParameter("id")));
		out = res.getWriter();
		if (delete) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}

	//修改用户
	@RequestMapping("/updateUser.do")
	public void update(User user, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		out = res.getWriter();
		try {
			userService.update(user);
			out.write("1");
			out.flush();
			out.close();
		} catch (Exception e) {
			out.write("0");
			out.flush();
			out.close();
		}
		
	}
	
	//
	@RequestMapping("/update.do")
	public void updateUser(User user, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		out = res.getWriter();
		try {
			userService.update(user);
			req.getSession().removeAttribute("user_online");
			req.getSession().setAttribute("user_online", user);
			out.write("1");
			out.flush();
			out.close();
		} catch (Exception e) {
			out.write("0");
			out.flush();
			out.close();
		}
		
	}

	//查找所有用户
	@RequestMapping("/findUser.do")
	public String findAllUser(Model mode) {
		List<User> list = userService.findAllUser();
		mode.addAttribute("users", list);
		return "user/user_list";
	}

	//通过ID查找用户
	@RequestMapping("/findById.do")
	public void findById(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("aaaaaaa");
		List<User> list = new ArrayList<User>();
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		jsonMap.put("total", 1);//total键 存放总记录数，必须的  
		list.add((User)req.getSession().getAttribute("user_online"));
        jsonMap.put("rows", list);//rows键 存放每页记录 list  

        JSONObject result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject
        out = res.getWriter();
		out.write(result.toString());
		System.out.println(result.toString());
		out.flush();
		out.close();
		
	}
	
	//分页列出用户
	@RequestMapping("/findByPage.do")
	public void findByPage(User user, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		String page = req.getParameter("page");
		String rows = req.getParameter("rows");
		System.out.println(page);
		
		List<User> list = null;
		if (null != user.getAccount()) {
			user.setAccount("%" + user.getAccount() + "%");
			list = userService.findByPage(
				Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
				Integer.parseInt(rows), user);
		} else {
			list = userService.findByPage(
					Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
					Integer.parseInt(rows));
		}
		System.out.println(list.toString());
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		jsonMap.put("total", userService.getSum());//total键 存放总记录数，必须的  
 
        jsonMap.put("rows", list);//rows键 存放每页记录 list  

        JSONObject result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject
        out = res.getWriter();
		out.write(result.toString());
		System.out.println(result.toString());
		out.flush();
		out.close();
	}
	
	//登录
	@RequestMapping("/login.do")
	public String login(User user, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		User u = userService.login(user);
		if (null != u) {
			System.out.println("登录成功");
			req.getSession().setAttribute("user_online", u);
			System.out.println(u.getLevel());
			if ("1".equals(u.getLevel())) {
				return "redirect:/pages/manage/index.jsp";
			} else {
				return "redirect:/pages/user/index.jsp";
			}
		} else {
			System.out.println("登录失败");
			return "redirect:/pages/manage/login.jsp";
		}
	}
	
	//退出
	@RequestMapping("/logout.do")
	public String logout(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
			req.getSession().removeAttribute("user_online");
			return "redirect:/pages/manage/login.jsp";
	}
}
